/**
 * ...
 * >author		João Vítor de Souza
 */

package com.cupcakeengine.core 
{
	import com.cupcakeengine.CKEngine;	
	import com.cupcakeengine.display.BitmapObject;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Rectangle;

	public class BitmapRenderer implements IUpdatable
	{
		// ___________________________________________________________________ CONSTANTS		
		
		// ___________________________________________________________________ CLASS PROPERTIES
		
		private var _vBitmapObject					: Vector.<BitmapObject>;
		private var _canvasBitmapData				: BitmapData;
		private var _canvasBitmap					: Bitmap;
		private var _canvasRectangle				: Rectangle;
		
		// ___________________________________________________________________ INSTANCE PROPERTIES
		
		private static var _instance	: BitmapRenderer;
		
		// ___________________________________________________________________ GETTERS AND SETTERS
		
		public function get numObjects():int { return _vBitmapObject.length; }
		
		// ___________________________________________________________________ CONSTRUCTOR
		
		public function BitmapRenderer (singleton:SingletonObligate) {}
		
		// ___________________________________________________________________ PUBLIC METHODS
		
		public static function getInstance () : BitmapRenderer
		{
			if (!BitmapRenderer._instance) BitmapRenderer._instance = new BitmapRenderer(new SingletonObligate());
			return BitmapRenderer(BitmapRenderer._instance);
		}		
		
		// ___________________________________________________________________ PUBLIC METHODS
		
		public function init(root:Sprite, width:int, height:int):void
		{ 			
			_vBitmapObject = new Vector.<BitmapObject>;
			
			_canvasRectangle = new Rectangle(0, 0, width, height);
			_canvasBitmapData = new BitmapData(width, height);
			_canvasBitmap = new Bitmap(_canvasBitmapData);
			
			root.addChild(_canvasBitmap);
			
			CKEngine.addUpdate(this, "renderer");
		}		
		
		public function add(bitmapObject:BitmapObject):void
		{
			_vBitmapObject.push(bitmapObject);
		}
		
		public function remove(bitmapObject:BitmapObject):void
		{
			if (_vBitmapObject.indexOf(bitmapObject) == -1) return;
			
			_vBitmapObject.splice(_vBitmapObject.indexOf(bitmapObject), 1);
		}
		
		public function setIndex(bitmapObject:BitmapObject, index:int):void
		{
			if (_vBitmapObject.indexOf(bitmapObject) == -1) return;
			
			_vBitmapObject.splice(_vBitmapObject.indexOf(bitmapObject), 1);
			
			if (index == _vBitmapObject.length) {
				_vBitmapObject.push(bitmapObject);
				return;
			}
			
			if (index == 0) {
				_vBitmapObject.unshift(bitmapObject);
				return;
			}
		}
		
		/* INTERFACE com.mifiz.core.IUpdatable */
		
		public function update():void 
		{	
			var len:int = _vBitmapObject.length;			
			
			_canvasBitmapData.fillRect(_canvasRectangle, 0xFFFFFF);
			
			var bitmapObject:BitmapObject;
			
			_canvasBitmapData.lock();
			
			for (var i:int = 0; i < len; i++) {
				bitmapObject = _vBitmapObject[i];
				_canvasBitmapData.copyPixels(bitmapObject.bitmapData, bitmapObject.rectangle, bitmapObject.point, bitmapObject.alpha, null, true);
			}
			
			_canvasBitmapData.unlock();
		}
		
		// ___________________________________________________________________ PRIVATE METHODS
		
		// ___________________________________________________________________ EVENTS
	}
}

class SingletonObligate {}